﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Net;
using System.Globalization;

namespace Skyper.Service {
	/// <summary>
	/// Interaction logic for MainWindow.xaml
	/// </summary>
	public partial class MainWindow : Window {
		#region Fields
		private ServiceHost host;
		#endregion
		#region Constructor
		public MainWindow() {
			InitializeComponent();
			this.Loaded += new RoutedEventHandler(MainWindow_Loaded);
			this.Unloaded += new RoutedEventHandler(MainWindow_Unloaded);
		}
		#endregion
		#region Private
		private void MainWindow_Unloaded(object sender, RoutedEventArgs e) {
			
		}
		private void MainWindow_Loaded(object sender, RoutedEventArgs e) {
			this.RefreshData();
		}
		private void buttonState_Checked(object sender, RoutedEventArgs e) {
			this.comboHostname.IsEnabled = false;
			this.textPort.IsEnabled = false;

			try {
				this.host = new ServiceHost(this.GetEndPoint());
				ServiceConfig.Instance.Hostname = comboHostname.Text;
				ServiceConfig.Instance.Port = textPort.Text;
				ServiceConfig.Instance.UseAnyIP = checkAny.IsChecked.Value;
				this.host.Start();
			} catch {
				MessageBox.Show("Unable to start server.", "Error", MessageBoxButton.OK, MessageBoxImage.Warning);
				this.buttonState.IsChecked = false;
			}
		}
		private void buttonState_Unchecked(object sender, RoutedEventArgs e) {
			this.comboHostname.IsEnabled = true;
			this.textPort.IsEnabled = true;

			if (host != null) { 
				this.host.Stop();
				this.host = null;
			}
		}
		private void textPort_TextChanged(object sender, TextChangedEventArgs e) {
			ushort port = 0;
			if (!ushort.TryParse(this.textPort.Text, NumberStyles.Number, NumberFormatInfo.InvariantInfo, out port)) {
				port = 6789;
			}
			e.Handled = true;
			this.textPort.Text = port.ToString();
			ServiceConfig.Instance.Port = port.ToString();
		}
		private void checkAny_Checked(object sender, RoutedEventArgs e) {
			ServiceConfig.Instance.UseAnyIP = true;
		}
		private void checkAny_Unchecked(object sender, RoutedEventArgs e) {
			ServiceConfig.Instance.UseAnyIP = false;
		}
		private void comboHostname_SelectionChanged(object sender, SelectionChangedEventArgs e) {
			//this.buttonState.IsEnabled = this.CanStart;
		}
		#endregion
		#region Protected
		protected void RefreshData() {
			var list = ServiceHost.GetAddress();

			this.comboHostname.ItemsSource = list;
			//this.buttonState.IsEnabled = this.CanStart;

			this.comboHostname.Text = ServiceConfig.Instance.Hostname;
			this.textPort.Text = ServiceConfig.Instance.Port;
			this.checkAny.IsChecked = ServiceConfig.Instance.UseAnyIP;
		}
		protected IPEndPoint GetEndPoint() {
			IPAddress adr;
			ushort port = 0;

			if (this.checkAny.IsChecked.Value) {
				adr = IPAddress.Any;
			} else {
				if (!IPAddress.TryParse(this.comboHostname.Text, out adr)) {
					return null;
				}
			}
			
			if (!ushort.TryParse(this.textPort.Text, NumberStyles.Number, NumberFormatInfo.InvariantInfo, out port)) {
				return null;
			}

			return new IPEndPoint(adr, port);
		}
		#endregion
		#region Property
		public Boolean CanStart {
			get {
				return (this.GetEndPoint() != null);
			}
		}
		#endregion
	}
}
